//
//  LuckyItem.m
//  SMTools
//
//  Created by fengsh on 2019/1/5.
//  Copyright © 2019 fengsh. All rights reserved.
//

#import "LuckyItem.h"
#import <Masonry/Masonry.h>
#import "LuckyResult.h"
#import <SDWebImage/SDWebImageManager.h>
#import "UIImageView+WebCache.h"
#import "LuckyUtils.h"

@interface LuckyItem ()

@end

@implementation LuckyItem

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    [self.img_avatar setWantsLayer:YES];
//    self.img_avatar.layer.backgroundColor = [NSColor greenColor].CGColor;
    self.img_avatar.layer.cornerRadius = 10;
    self.img_avatar.layer.masksToBounds = YES;
    
    self.phone.font = [NSFont systemFontOfSize:18];
    self.phone.textColor = [NSColor whiteColor];
    self.phone.alignment = NSTextAlignmentCenter;
    
    self.name.font = [NSFont boldSystemFontOfSize:22];
    self.name.textColor = [NSColor whiteColor];
    self.name.alignment = NSTextAlignmentCenter;
    
    [self.img_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.centerX.equalTo(@(0));
        make.width.equalTo(self.img_avatar.mas_height);
        make.bottom.equalTo(self.name.mas_top).offset(-5);
    }];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.phone.mas_top);
        make.centerX.equalTo(@(0));
        make.leading.equalTo(@(10));
        make.trailing.equalTo(@(-10));
    }];
    
    [self.phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(-10));
        make.centerX.equalTo(@(0));
        make.leading.equalTo(@(10));
        make.trailing.equalTo(@(-10));
    }];
}

- (void)dealloc
{
    NSLog(@"LuckyItem free !");
}

-(void)setRepresentedObject:(id)representedObject
{
    [super setRepresentedObject:representedObject];
    if (representedObject !=nil)
    {
        LuckyResult *result = representedObject;
        
        NSString *photo = result.photo;
        if (photo.length > 0) {
//            NSLog(@"p = %@",photo);
            
//           dispatch_async(dispatch_queue_create("com.fsh.downloader", DISPATCH_QUEUE_CONCURRENT), ^{
            NSURL *av = [NSURL URLWithString:photo];
////            NSLog(@"p = %@",av);
////               SDImageCacheQueryDiskSync
////               [SDWebImageManager sharedManager].imageCache.
//
//            [[SDWebImageManager sharedManager]loadImageWithURL:av options:SDWebImageRetryFailed|SDWebImageQueryDiskSync progress:nil completed:^(NSImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
//
//                NSLog(@"%@",NSStringFromRect(self.img_avatar.frame));
//                self.img_avatar.image = [LuckyUtils resizeImage:image size:self.img_avatar.frame.size];
//            }];
//           });
//            [self.img_avatar sd_setImageWithURL:av completed:^(NSImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//                self.img_avatar.image = [LuckyUtils resizeImage:image size:self.img_avatar.frame.size];
//            }];
            dispatch_async(dispatch_queue_create("com.fsh.downloader", DISPATCH_QUEUE_CONCURRENT), ^{
                NSError *err = nil;
                NSData *dt = [NSData dataWithContentsOfURL:av options:NSDataReadingMappedAlways error:&err];
                NSLog(@"dt = %@,error = %@",dt,err);

//                dispatch_async(dispatch_get_main_queue(), ^{ //这里在模态窗不退出的情况下是不走的。
                    NSImage *img = [[NSImage alloc]initWithData:dt];
//                    self.img_avatar.image = img;
//                });
                [self performSelectorOnMainThread:@selector(onUpdateAvatar:) withObject:img waitUntilDone:YES];
            });
            
        }
        
        NSString *name = result.name?:@"";
        NSString *mobile = result.phone?:@"";
        
        [self.phone setStringValue:mobile];
        [self.name setStringValue:name];
        
    }
    else
    {
        [self.phone setStringValue:@""];
        [self.name setStringValue:@""];
    }
}

- (void)onUpdateAvatar:(NSImage *)img
{
    self.img_avatar.image = [LuckyUtils resizeImage:img size:self.img_avatar.frame.size];//img;
}

@end
